<?php 
class districtsAction extends AdminMaster{
	public function listFilter(){
		$this->data['filter']['areas']	= LocationAreas::getDataListFrontEnd();
		$this->data['filter']['area_id'] = $this->request->getParameter('area_id', -1);
		$this->data['filter']['province_id'] = $this->request->getParameter('province_id', -1);
		
	}
	public function showList(){
		$this->data['list']			 		= LocationDistricts::getDataList($this->data['filter'], $this->data['pagination']);
		$this->data['pagination']['total']		= LocationDistricts::getDataListCount($this->data['filter']);
		$this->data['pagination']['router']		= 'location_districts';
		$this->data['pagination']['params']		= $this->data['filter'];
	}
	public function delRecord(){
		LocationDistrictsTable::delete($this->data['select_list']);
	}
	public function disableRecord(){
		LocationDistrictsTable::disable($this->data['select_list']);
	}
	public function enableRecord(){
		LocationDistrictsTable::enable($this->data['select_list']);
	}
	public function saveOrder(){
		$sortData = array();
		for($i = 0; $i< count($this->data['ids']); $i++){
			$sortData[$this->data['ids'][$i]] = $this->data['sort_orders'][$i];
		}
		LocationDistricts::saveSortOrder($sortData);
	}
	public function addnew(){
		$this->setTemplate('districtsForm');

		$form = new LocationDistrictsFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			
			if($form->isValid()){
				$product_id = LocationDistricts::insertUpdate($this->formData);
				if($product_id > 0){
					$this->data['result']	= __('add_new_success');
					$form = new LocationDistrictsFForm();
				}else{
					$this->data['result']	= sprintf(__('add_new_error'), sfConfig::get('app_email_admin')) ;
				}
			}
		}
		$this->setVar('form', $form);
	}
	public function edit(){
		$this->setTemplate('districtsForm');

		$form = new LocationDistrictsFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(LocationDistricts::insertUpdate($this->formData)){
					$this->data['result']	= __('edit_success');
				}else{
					$this->data['result']	= sprintf(__('edit_error'), sfConfig::get('app_email_admin')) ;
				}
			}
		}else{
			$data = LocationDistricts::getDataByIdAdmin($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				$this->formData = array(
						'location_province_id'	=> intval($data['location_province_id']),
						'is_enabled'		=> $data['is_enabled'],
						'_csrf_token'=> $form->getCSRFToken()
				);
				$this->data['filter']['area_id'] = $data['LocationProvinces']['LocationAreas']['id'];
				$this->data['filter']['province_id'] = $data['LocationProvinces']['id'];
				
				foreach($this->data['langs'] as $lang){
					$this->formData['name_'.$lang] = $data['LocationDistrictsI18n'][$lang]['name'];
					$this->formData['description_'.$lang] = $data['LocationDistrictsI18n'][$lang]['description'];
					
				}
				$form->bind($this->formData);

			}
		}
		$this->setVar('form', $form);
	}
}